<template>
	<!-- 自动任务 -->
	<div>
		<v-container row class="text-subtitle-1 d-flex justify-center  align-left">

			<v-btn class="ml-2 mt-7" small tile color="primary" @click="addAutoTaskDialog=true,taskModel.id=0">
				添加自动任务
			</v-btn>
			<v-btn class="ml-3 mt-7" small tile text color="primary" @click="addAutoTaskDialog=true,taskModel.id=0">
				任务调度中心状态:<a>{{centerStatus}}</a>
			</v-btn>
			<v-divider class="mx-4" inset vertical></v-divider>
			<v-row class="mt-2">
				<v-col cols="12" sm="3" md="2">
					<v-text-field label="任务名称" v-model="taskQuery.name" dense outlined>
					</v-text-field>
				</v-col>
				<!-- sm="6" md="2" -->
				<v-col cols="12" sm="3" md="2">
					<v-text-field label="任务分组" v-model="taskQuery.jobgroup" dense outlined>
					</v-text-field>
				</v-col>
				<!-- ml-n16 前面加n是间距 -->
				<!-- class="ml-n16 mx-auto" -->
				<v-col cols="12" sm="2" md="2">
					<v-btn depressed color="primary" @click="loadPages">查询</v-btn>
					<v-btn class="ml-3" depressed color="primary" @click="refreshLoadPages">
						刷新</v-btn>
				</v-col>
			</v-row>
		</v-container>
		<br />
		<!-- :single-select="true" -->
		<v-data-table :headers="taskHeaderList" :items="taskDataList" small fixed-header item-key="id" dense
			class="text-no-wrap" loading-text="正在加载请稍等......" :options.sync="taskOptions"
			:footer-props="vuetifyDefaultPage" :loading="taskLoading" :server-items-length="taskTotalCount">
			<template v-slot:item.isstart="{ item }">
				<a v-if="item.isstart" style="color: #67C23A;">已启动</a>
				<a v-else>未启动</a>
			</template>
			<template v-slot:item.triggertype="{ item }">
				{{item.triggertype==1?"CRON表达式":"普通自定义"}}
			</template>
			<template v-slot:item.isfixednumber="{ item }">
				<a v-if="item.isfixednumber" style="color: red;">已执行指定次数：{{item.runtimes}}次</a>
				<a v-else>否</a>
			</template>
			<!--  -->
			<template v-slot:item.description="{ item }">
				<a :title="item.description" class="text-overflows">{{item.description}}</a>
			</template>
			<template v-slot:item.actions="{ item }">
				<div>
					<v-btn small tile color="success" @click="addAutoTaskDialog=true, taskModel=item">
						编辑
					</v-btn>
					<v-btn class="ml-2" small tile color="primary" @click="StartJob(item.id)">
						启动
					</v-btn>
					<v-btn class="ml-2" small tile color="primary" @click="StopJob(item.id)">
						暂停
					</v-btn>
					<v-btn class="ml-2" small tile color="primary" @click="ReCovery(item.id)">
						重启
					</v-btn>
					<v-btn class="ml-2" small tile color="red" @click="Delete(item.id)">
						删除
					</v-btn>
				</div>
			</template>
		</v-data-table>
		<v-dialog v-model="addAutoTaskDialog" max-width="800px">
			<v-card>
				<v-card-title>添加自动任务</v-card-title>
				<v-card-text>
					<v-alert text color="blue-grey">
						1.触发器类型默认为【普通自定义】，如需复杂的定时规则，可选择CRON表达式
						<br />
						2.不输入结束日期，则默认结束日期为最大的结束日期，表示永远执行下去
						<br />
						3.程序集名称就是类库名称，如:TR.Automation.AutoTasks,任务所在类如:QuartzNet.Jobs.TestJob(项目下的文件层级.类名)
					</v-alert>
					<v-row>
						<v-col cols="12" sm="2" md="4">
							<v-text-field label="任务名称(必填)" v-model="taskModel.name" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-text-field label="任务程序集名称(必填)" v-model="taskModel.assemblyname" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-text-field label="任务所在类(必填)" v-model="taskModel.classname" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-text-field label="任务间隔时间/秒(必填)" v-model="taskModel.intervalsecond" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-text-field label="任务执行传参" v-model="taskModel.jobparams" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-text-field label="任务分组名称" v-model="taskModel.jobgroup" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-menu v-model="begintimeMenu" :close-on-content-click="false" :nudge-right="40"
								transition="scale-transition" offset-y min-width="auto">
								<template v-slot:activator="{ on, attrs }">
									<v-text-field required v-model="taskModel.begintime" label="开始日期(必填)" readonly
										v-bind="attrs" v-on="on" dense outlined>
									</v-text-field>
								</template>
								<v-date-picker locale="zh-cn" v-model="taskModel.begintime"
									@input="begintimeMenu=false,begintimeMinutes=true">
								</v-date-picker>
							</v-menu>
							<v-menu v-model="begintimeMinutes" :close-on-content-click="false" :nudge-right="40"
								transition="scale-transition">
								<v-time-picker format="24hr" use-seconds @input="startTimePickChangeEvent">
								</v-time-picker>
							</v-menu>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-menu v-model="endtimeMenu" :close-on-content-click="false" :nudge-right="40"
								transition="scale-transition" offset-y min-width="auto">
								<template v-slot:activator="{ on, attrs }">
									<v-text-field required v-model="taskModel.endtime" label="结束日期" readonly
										v-bind="attrs" v-on="on" dense outlined>
									</v-text-field>
								</template>
								<v-date-picker locale="zh-cn" v-model="taskModel.endtime"
									@input="endtimeMenu=false,endtimeMenuMinutes=true">
								</v-date-picker>
							</v-menu>
							<v-menu v-model="endtimeMenuMinutes" :close-on-content-click="false" :nudge-right="40"
								transition="scale-transition" offset-y min-width="auto">
								<v-time-picker format="24hr" use-seconds @input="endTimePickChangeEvent">
								</v-time-picker>
							</v-menu>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-text-field label="任务执行传参" v-model="taskModel.jobparams" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-select v-model="taskModel.triggertype" :items="triggertypes" label="触发器类型" dense
								outlined>
							</v-select>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-text-field label="任务将要执行次数" v-model="taskModel.waittimes" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12" sm="2" md="4">
							<v-text-field label="任务运行时间表达式" v-model="taskModel.cron" dense outlined>
							</v-text-field>
						</v-col>
						<v-col cols="12">
							<v-textarea outlined name="input-7-4" label="描述" v-model="taskModel.remark"
								:value="taskModel.remark"></v-textarea>
						</v-col>
					</v-row>
				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn class="mr-4" tile color="success" @click="submit">
						提交
					</v-btn>
					<v-btn color="secondary" tile dark @click="cannel">
						取消
					</v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
	</div>
</template>

<script>
	export default {
		name: 'auto_tasks',

		data() {
			return {
				//开始日期
				begintimeMenu: false,
				//开始时间
				begintimeMinutes: false,
				endtimeMenu: false,
				endtimeMenuMinutes: false,
				addAutoTaskDialog: false,
				taskQuery: {
					name: '',
					jobgroup: '',
				},
				triggertypes: [{
						text: '普通自定义',
						value: 0,
					},
					{
						text: 'CRON表单式',
						value: 1
					}
				],
				taskHeaderList: [],
				taskDataList: [],
				taskOptions: { //页数
					page: 1,
					//每页行数
					itemsPerPage: 10
				},
				taskLoading: true,
				taskTotalCount: 0,
				vuetifyDefaultPage: {
					'items-per-page-text': '每页个数',
					'items-per-page-options': [10, 20, 50, 100]
					// 'items-per-page-all-text': '100'
				},
				taskModel: {},
				centerStatus: '',
			}
		},
		created() {
			this.getCenterStatus()
			this.GetHeader()
		},
		watch: {
			taskOptions: {
				handler() {
					this.loadPages()
				},
				deep: true
			},
		},
		methods: {
			async loadPages() {
				this.getCenterStatus()
				this.taskLoading = true
				var response = await this.$httpRequest({
					url: `SPM/AutoTasks/LoadPages?
					name=${this.taskQuery.name}&
					jobgroup=${this.taskQuery.jobgroup}&
					pageNumber=${this.taskOptions.page}&
					pageSize=${this.taskOptions.itemsPerPage}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.taskDataList = response.data.result
					this.taskTotalCount = response.data.pagination.totalRecords
					this.taskLoading = false
				} else {
					this.$message.error(response.info)
				}
			},
			async refreshLoadPages() {
				this.taskQuery.name = ''
				this.taskQuery.jobgroup = ''
				await this.loadPages()
			},
			async getCenterStatus() {
				var response = await this.$httpRequest({
					url: `SPM/AutoTasks/SchedulerCenterStatus`,
					method: 'get',
				})
				if (response.status == 1) {
					this.centerStatus = '开启'
				} else {
					this.centerStatus = '未开启'
				}
			},
			async StartJob(jobId) {
				var response = await this.$httpRequest({
					url: `SPM/AutoTasks/StartJob?jobId=${jobId}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					await this.loadPages()
				} else {
					this.$message.error(response.info)
				}
			},
			async StopJob(jobId) {
				var response = await this.$httpRequest({
					url: `SPM/AutoTasks/StopJob?jobId=${jobId}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					await this.loadPages()
				} else {
					this.$message.error(response.info)
				}
			},
			async ReCovery(jobId) {
				var response = await this.$httpRequest({
					url: `SPM/AutoTasks/ReCovery?jobId=${jobId}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					await this.loadPages()
				} else {
					this.$message.error(response.info)
				}
			},
			async Delete(jobId) {
				var response = await this.$httpRequest({
					url: `SPM/AutoTasks/Delete/${jobId}`,
					method: 'delete',
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					await this.loadPages()
				} else {
					this.$message.error(response.info)
				}
			},
			async submit() {
				var url = ''
				var method = ''
				if (this.taskModel.id == 0) {
					url = `SPM/AutoTasks/Post`
					method = 'Post'
				} else {
					url = `SPM/AutoTasks/Put`
					method = 'Put'
				}
				var response = await this.$httpRequest({
					url: url,
					method: method,
					data: this.taskModel
				})

				if (response.status == 1) {
					await this.loadPages()
					this.$message.success(response.info)
					this.addAutoTaskDialog = false
				} else {
					this.$message.error(response.info)
				}
			},
			startTimePickChangeEvent(time) {
				var splits = this.taskModel.begintime.split(' ')
				this.taskModel.begintime = splits[0] + ' ' + time
			},
			endTimePickChangeEvent(time) {
				var splits = this.taskModel.endtime.split(' ')
				this.taskModel.endtime = splits[0] + ' ' + time
			},
			cannel() {
				this.taskModel = {}
				this.addAutoTaskDialog = false
			},
			GetHeader() {
				this.taskHeaderList = [{
						text: '操作',
						align: 'center',
						value: 'actions',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '任务名称',
						align: 'center',
						value: 'name',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '任务分组',
						align: 'center',
						value: 'jobgroup',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '执行传参',
						align: 'center',
						value: 'jobparams',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '是否启动指定次数',
						align: 'center',
						value: 'isfixednumber',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '是否启动',
						align: 'center',
						value: 'isstart',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '开始时间',
						align: 'center',
						value: 'begintime',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '结束时间',
						align: 'center',
						value: 'endtime',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '触发器类型',
						align: 'center',
						value: 'triggertype',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '执行间隔时间/秒',
						align: 'center',
						value: 'intervalsecond',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '任务运行时间表达式',
						align: 'center',
						value: 'cron',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '程序集名称',
						align: 'center',
						value: 'assemblyname',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '任务所在类名',
						align: 'center',
						value: 'classname',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '任务将要执行次数',
						align: 'center',
						value: 'waittimes',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '累计执行次数',
						align: 'center',
						value: 'runtimes',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '描述',
						align: 'center',
						value: 'remark',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '任务执行结果',
						align: 'center',
						value: 'description',
						filterable: false,
						class: 'fixed'
					},
				]
			},
		},
	}
</script>

<style>
	.text-overflow {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.text-overflows {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 3;
		overflow: hidden;
		width:200px;
	}
</style>
